
package system.base;

import framework.core.*;
import system.data.*;
/*
	Author    :Wing
	CreateTime:2008-02-04 10:46:45
*/
public class MenuSrv extends IHandler
{

	public MenuSrv() 
	{
		super();
	}
	private void deleteMenu(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		Menu obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = Menu.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveMenu(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		Menu obj;
		if((id==null)||(id.equals("")))   //add
		{
			obj = Menu.fetch();
    	}
    	else                     //update
    	{
    		obj = Menu.fetch(con,id);
    	}
		MenuPageData data = (MenuPageData)PageData.fromForm(context,MenuPageData.class);
		context.setValue("MenuPageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listMenu(DBConnection con,SrvContext context,String path) throws Exception
	{
		MenuSearchEvent event = (MenuSearchEvent)SearchEvent.fromForm(context,MenuSearchEvent.class);
		context.setSessionValue("MenuSearchEvent",event);
		context.setNextUrl(path+"MenuList.jsp");
	}
	private void backMenu(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"MenuList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listMenu"))
		{
			listMenu(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewMenu"))
		{
			String id = context.getString("Id",null);
			Menu obj = Menu.fetch(con,id);
			context.setValue("Menu",obj);
			context.setNextUrl(path+"MenuView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteMenu"))
		{
			context.setErrorUrl(path+"MenuList.jsp");
			deleteMenu(con,context);
			backMenu(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddMenu"))
		{
			Menu obj = Menu.fetch();
			MenuPageData data = (MenuPageData)PageData.fromTable(obj,MenuPageData.class);
			context.setValue("MenuPageData",data);
			context.setNextUrl(path+"MenuEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateMenu"))
		{
			String id = context.getString("Id",null);
			Menu obj = Menu.fetch(con,id);
			MenuPageData data = (MenuPageData)PageData.fromTable(obj,MenuPageData.class);
			context.setValue("MenuPageData",data);
			context.setNextUrl(path+"MenuEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyMenu"))
		{
			String id = context.getString("Id",null);
			Menu obj = Menu.copy(Menu.fetch(con,id));
			MenuPageData data = (MenuPageData)PageData.fromTable(obj,MenuPageData.class);
			context.setValue("MenuPageData",data);
			context.setNextUrl(path+"MenuEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveMenu"))
		{
			context.setErrorUrl(path+"MenuEdit.jsp");
			saveMenu(con,context,loginContext);
			backMenu(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewMenu"))
		{
			context.setErrorUrl(path+"MenuEdit.jsp");
			saveMenu(con,context,loginContext);
			Menu obj = Menu.fetch();
			MenuPageData data = (MenuPageData)PageData.fromTable(obj,MenuPageData.class);
			context.setValue("MenuPageData",data);
			context.setNextUrl(path+"MenuEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListMenu"))
		{
			backMenu(con,context,path);
			return;
		}
		
		//add by Wing start

		//add by Wing end

		Logger.throwException("MenuSrv","unknown action_code="+action_code);
	}
}
